package com.yunji.cabinet.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yunji.cabinet.bo.UpgradeTaskListBO;
import com.yunji.cabinet.dao.IUpgradeTaskDao;
import com.yunji.cabinet.domain.UpgradeTaskDO;
import com.yunji.cabinet.enums.UpgradeTaskStatus;
import com.yunji.cabinet.mapper.UpgradeTaskMapper;
import com.yunji.cabinet.params.UpgradeTaskParam;
import com.yunji.cabinet.task.UpgradeTask;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.base.IModifyStatusBaseBO;
import com.yunji.common.utils.TimeUtils;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UpgradeTaskDaoImpl extends BaseDaoImpl<UpgradeTaskMapper, UpgradeTaskDO> implements IUpgradeTaskDao {
    @Override
    public boolean modifyStatus(IModifyStatusBaseBO bo) {
        return modifyStatusById(bo,UpgradeTaskDO::getTaskCode,UpgradeTaskDO::getTaskStatus);
    }

    @Override
    public boolean startTask(String taskCode) {
        return update(new LambdaUpdateWrapper<UpgradeTaskDO>()
                .set(UpgradeTaskDO::getStartTime, TimeUtils.getNowDate())
                .eq(UpgradeTaskDO::getTaskCode, taskCode));
    }

    @Override
    public boolean closeTask(String taskCode) {
        return update(new LambdaUpdateWrapper<UpgradeTaskDO>()
                .set(UpgradeTaskDO::getCloseTime, TimeUtils.getNowDate())
                .eq(UpgradeTaskDO::getTaskCode, taskCode));
    }

    @Override
    public List<UpgradeTaskDO> searchPageList(UpgradeTaskParam param) {
        return pageList(param,new LambdaQueryWrapper<UpgradeTaskDO>()
                .eq(BeanUtils.isNotNull(param.getTaskCode()),UpgradeTaskDO::getTaskCode,param.getTaskCode())
                .eq(BeanUtils.isNotNull(param.getChipType()),UpgradeTaskDO::getChipType,param.getChipType())
                .eq(BeanUtils.isNotNull(param.getTaskStatus()),UpgradeTaskDO::getTaskStatus,param.getTaskStatus())
                .like(BeanUtils.isNotNull(param.getTaskName()),UpgradeTaskDO::getTaskName,param.getTaskName())
                .like(BeanUtils.isNotNull(param.getVersion()),UpgradeTaskDO::getVersion,param.getVersion())
                .orderByDesc(UpgradeTaskDO::getCreateTime));
    }

    @Override
    public List<UpgradeTaskDO> search(UpgradeTaskListBO bo) {


        return list(new LambdaQueryWrapper<UpgradeTaskDO>()
                .eq(BeanUtils.isNotNull(bo.getTaskStatus()),UpgradeTaskDO::getTaskStatus,bo.getTaskStatus())
                .orderByAsc(UpgradeTaskDO::getCreateTime));
    }
}
